package com.tct.smartRE.fwk.impl.service;

import java.util.List;
import java.util.Map;

import com.tct.smartRE.fwk.api.IBusinessObject;
import com.tct.smartRE.fwk.api.IQueryCondition;
import com.tct.smartRE.fwk.api.service.ICommonDBService;
import com.tct.smartRE.fwk.api.service.IDBServiceWrapper;
import com.tct.smartRE.fwk.api.transform.IDataTransformation;

public class DBServiceWrapper implements IDBServiceWrapper {

	private final ICommonDBService commonDBService;
	private final IDataTransformation dataTransformation;

	public DBServiceWrapper(ICommonDBService commonDBService, IDataTransformation dataTransformation) {
		this.commonDBService = commonDBService;
		this.dataTransformation = dataTransformation;
	}

	public Integer countAll(Class<?> clazz) {
		return commonDBService.countAll(clazz);
	}

	public void delete(IBusinessObject object) {
		commonDBService.delete(object);
	}

	public void delete(Integer id, Class<?> clazz) {
		commonDBService.delete(id, clazz);
	}

	public List<IBusinessObject> findAll(Class<?> clazz) {
		return commonDBService.findAll(clazz);
	}

	public IBusinessObject load(Integer id, Class<?> clazz) {
		return commonDBService.load(id, clazz);
	}

	public Integer save(Map<String, Object> param) {
		
		IBusinessObject realObject = dataTransformation.transformForSave(param);
		Integer result = commonDBService.save(realObject);
		
		return result;
	}

	public Integer count(Map<String, Object> param) {

		IQueryCondition queryCondition = dataTransformation.transformForCount(param);
		int result = commonDBService.countByCondition(queryCondition);
		
		return result;
	}
	
	public List<Map<String, Object>> find(Map<String, Object> param) {
		
		IQueryCondition queryCondition = dataTransformation.transformForFind(param);
		List<IBusinessObject> lst = commonDBService.findByCondition(queryCondition);
		List<Map<String, Object>> result = dataTransformation.deTransformForFind(lst);
		
		return result;
	}
}
